package com.lock.immersive.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.MerchantMapper;
import com.lock.immersive.mapper.UserMapper;
import com.lock.immersive.pojo.domain.Merchant;
import com.lock.immersive.pojo.domain.User;
import com.lock.immersive.pojo.dto.MerchantDto;
import com.lock.immersive.pojo.dto.MerchantRecommendDto;
import com.lock.immersive.pojo.enums.Identity;
import com.lock.immersive.service.MerchantService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.JWT;
import com.lock.immersive.utils.ResponseApi;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
* @author zhao
* @description 针对表【merchant】的数据库操作Service实现
* @createDate 2023-07-02 00:04:03
*/
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant>
    implements MerchantService {
    @Resource
    public MerchantMapper merchantMapper;
    @Resource
    public UserMapper userMapper;
    @Override
    public ResponseApi<Object> insertMerchant(MerchantDto merchantDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = JWT.getTokenId(token);
        User user = userMapper.selectById(userId);
        //成为超级管理员
        user.setIdentity(Identity.SUPERMANAGER);
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantDto, merchant);
        int insert = merchantMapper.insert(merchant);
        user.setMerchantId(insert);
        userMapper.updateById(user);
        if(insert>0){
            return ResponseApi.success();
        }else{
            throw new BusinessException(ErrorCode.INSERT_ERROR);        }
    }

    @Override
    public ResponseApi<Object> updateRecommend(MerchantRecommendDto merchantRecommendDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = JWT.getTokenId(token);
        Integer merchantId = merchantRecommendDto.getMerchantId();
        Merchant merchant = merchantMapper.selectById(merchantId);
        User user = userMapper.selectById(userId);
        if(Objects.equals(user.getMerchantId(), merchantId)){
            merchant.setRecommendList(merchantRecommendDto.getRecommendList()+"");
            int i = merchantMapper.updateById(merchant);
            if(i>0){
                return ResponseApi.success();
            }else{
                throw new BusinessException(ErrorCode.UPDATE_ERROR);
            }
        }else{
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
    }

    @Override
    public ResponseApi<Object> updateMerchant(Merchant merchant, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = JWT.getTokenId(token);
        User user = userMapper.selectById(userId);
        if(Objects.equals(user.getMerchantId(), merchant.getMerchantId())){
            int i = merchantMapper.updateById(merchant);
            if(i>0){
                return ResponseApi.success();
            }else{
                throw new BusinessException(ErrorCode.UPDATE_ERROR);
            }
        }else{
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
    }

}




